#!/bin/bash

hostname="shouchuang"
curl http://ipaddress:8081/gethost > ip.txt
ip=`cat ip.txt`
rm ip.txt
len=${#ip}
if [ $len -gt 15 ] || [ $len -lt 10 ]; then
    echo $ip
    exit
fi
newline="$ip    $hostname"
content=""
hasold=false
IFS_old=$IFS
IFS=$'\n'
for line in `cat /etc/hosts`;do
    include=$(echo $line |grep "${hostname}")
    if [ "$include" != "" ]; then 
        content=$content$newline"\n"
        hasold=true
    else 
        content=$content$line"\n"
    fi
done
IFS=$IFS_old
if [ $hasold == false ]; then
    content=$content$newline
fi
echo -e "$content" > /etc/hosts
echo "已将"$newline"写入hosts文件"